package com.xxwy.segmenttree;

import java.io.ObjectInputStream;

/**
 * @author xxwy
 */
public class SegmentTree<E> {

    private E[] data;
    private E[] tree;
    private Meager<E> meager;

    public SegmentTree(E[] arr, Meager meager) {
        data = (E[]) new Object[arr.length];
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        tree = (E[]) new Object[4 * arr.length];
        this.meager = meager;
        builderSegmentTree(0, 0, data.length - 1);
    }

    /**
     * 在treeIndex的位置创建表示区间[l...r]的线段树
     *
     * @param treeIndex : 节点index
     * @param l         区间左
     * @param r         区间右
     */
    private void builderSegmentTree(int treeIndex, int l, int r) {
        //子节点
        if (l == r) {
            tree[treeIndex] = data[l];
            return;
        }
        int leftTree = leftChild(treeIndex);
        int rightChild = rightChild(treeIndex);
        //int mid = (l + r) / 2; 会发生溢出
        int mid = l + (r - l) / 2;
        builderSegmentTree(leftTree, l, mid);
        builderSegmentTree(rightChild, mid + 1, r);
        //和业务相关
        tree[treeIndex] = meager.merge(tree[leftTree], tree[rightChild]);

    }

    /**
     * 区间queryL,queryR的值
     *
     * @param queryL
     * @param queryR
     * @return
     */
    public E query(int queryL, int queryR) {
        if (queryL < 0 || queryL >= data.length || queryR < 0 || queryR >= data.length || queryL > queryR) {
            throw new IllegalArgumentException("Index is illegal");
        }

        return query(0, 0, data.length - 1, queryL, queryR);
    }

    /**
     * 在以treeId为根的线段树中[l..r]的范围里，搜索区间,[queryL,,,queryR]的值
     *
     * @param treeIndex
     * @param l
     * @param r
     * @param queryL
     * @param queryR
     * @return
     */
    private E query(int treeIndex, int l, int r, int queryL, int queryR) {
        if (l == queryL && r == queryR) {
            return tree[treeIndex];
        }
        int mid = l + (r - l) / 2;
        int leftIndex = leftChild(treeIndex);
        int rightIndex = rightChild(treeIndex);
        if(queryL >= mid+1){
            //区间在右子树
            return query(rightIndex,mid+1,r,queryL,queryR);
        }else if(queryR <=mid){
            //区间在左子树
            return query(leftIndex,l,mid,queryL,queryR);
        }else {
            E query = query(leftIndex, l, mid, queryL, mid);
            E query2 = query(rightIndex, mid+1, r, mid+1, queryR);
            return meager.merge(query,query2);
        }
    }

    /**
     * 更新
     * @param index
     * @param e
     */
    public void set(int index,E e){
        if(index <0 || index >=data.length){
            throw new IllegalArgumentException("index is illegal");
        }
        data[index] = e;
        set(0,0,data.length,index,e);
    }

    /**
     * 在以treeIndex为根的线段树更新Index的值为e
     * @param treeIndex
     * @param l
     * @param r
     * @param index
     * @param e
     */
    private void set(int treeIndex, int l, int r, int index,E e) {
        if(l == r){
            tree[treeIndex] = e;
            return;
        }
        int mid = l + (r-l)/2;
        int leftChild = leftChild(treeIndex);
        int rightChild = rightChild(treeIndex);
        if(index >=mid+1){
            set(rightChild,mid+1,r,index,e);
        }else{
            set(leftChild,l,mid,index,e);
        }
        tree[treeIndex] = meager.merge(tree[leftChild] , tree[rightChild]);
    }

    public E get(int index) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("index is illegal");
        }
        return data[index];
    }

    public int getSize() {
        return data.length;
    }

    private int leftChild(int index) {
        return 2 * index + 1;
    }

    private int rightChild(int index) {
        return 2 * index + 2;
    }

    private int parent(int index) {
        if (index == 0) {
            throw new IllegalArgumentException("index-0 doesn't have parent");
        }
        return (index - 1) / 2;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append('[');
        for (int i = 0; i < tree.length; i++) {
            if (tree[i] != null) {
                res.append(tree[i]);
            } else {
                res.append("null");
            }
            if (i != tree.length - 1) {
                res.append(", ");
            }

        }
        res.append(']');
        return res.toString();
    }

}
